#include <fal.h>
#include "flash.h"


/**
 * Get the sector of a given address
 *
 * @param address flash address
 *
 * @return The sector of a given address
 */

static int init(void)
{
    /* do nothing now */
}

static int read(long offset, uint8_t* buf, size_t size)
{
    size_t i;
    uint32_t addr = ab32vg1_onchip_flash.addr + offset;
    FlashRead(addr, buf, size);
    return size;
}

static int write(long offset, const uint8_t* buf, size_t size)
{
    size_t i;
    uint32_t read_data;
    uint32_t addr = ab32vg1_onchip_flash.addr + offset;
    FlashWrite(addr, buf, size);
    return size;
}

static int erase(long offset, size_t size)
{

    FlashErase(offset, size);
    return size;
}

const struct fal_flash_dev ab32vg1_onchip_flash =
{
    .name = "AB32_onchip",
    .addr = DATA_ADDRESS_START,
    .len = DATA_ADDRESS_END - DATA_ADDRESS_START,
    .blk_size = 512,
    .ops = {init, read, write, erase},
    .write_gran = 8
};
